package com.springboot.film.controller;

import com.springboot.film.entity.Page;
import com.springboot.film.entity.films;
import com.springboot.film.service.filmsService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
public class filmsCtrl {

    @Autowired
    private filmsService filmsService;

    //分页显示全部
    @RequestMapping("/houtai/films")
    public String films(Model model, String pageNow, String nosearch) {
        int totalCount = 0;
        totalCount = filmsService.getCount();
        List<films> filmsList=new ArrayList<films>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            filmsList=filmsService.findByPage(page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            filmsList=filmsService.findByPage(page.getStartPos(), page.getPageSize());
        }
        model.addAttribute("page", page);
        model.addAttribute("top", "top_films");
        model.addAttribute("show_left", "left_films");
        model.addAttribute("left", "left_y_films");
        model.addAttribute("mypage", "films");
        model.addAttribute("films", filmsList);
        if(nosearch != null){
            model.addAttribute("nosearch", nosearch);
        }
        return "houtai_films";
    }

    //分页显示全部搜索
    @RequestMapping("/houtai/searchfilms")
    public String searchfilms(Model model, String pageNow,int types,String keyword) {
        int totalCount = 0;
        String keyword_new="name";
        if(types == 1) {
            keyword_new = "name";
        }else if (types == 2) {
            keyword_new = "type";
        }
        totalCount = filmsService.getsearchCount(keyword_new,keyword);
        List<films> filmsList=new ArrayList<films>();
        Page page=null;
        if(pageNow!=null){
            page=new Page(totalCount, Integer.parseInt(pageNow));
            filmsList=filmsService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }else{
            page=new Page(totalCount, 1);//初始化pageNow为1
            filmsList=filmsService.findsearchByPage(keyword_new,keyword,page.getStartPos(), page.getPageSize());
        }
        if(filmsList != null && totalCount != 0){
            model.addAttribute("page", page);
            model.addAttribute("top", "top_films");
            model.addAttribute("show_left", "left_films");
            model.addAttribute("left", "left_y_films");
            model.addAttribute("mypage", "search&types="+types+"&keyword="+keyword+"");
            model.addAttribute("films", filmsList);
            return "houtai_films";
        }else{
            model.addAttribute("nosearch", "nosearch");
            return "redirect:/houtai/films?nosearch=nosearch";
        }
    }

    //添加
    //接收并返回json格式数据
    @RequestMapping("/houtai/addfilms")
    @ResponseBody
    public String addfilms(films films) {
        filmsService.add(films);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code", "1");
        return jsonObject.toString();
    }

    //更改
    @RequestMapping("/houtai/updatefilms")
    public String updatefilms(films films) {
        filmsService.update(films);
        return "redirect:/houtai/films";
    }

    //删除
    @RequestMapping("/houtai/deletefilms")
    public String deletefilms(int userid) {
        filmsService.delete(userid);
        return "redirect:/houtai/films";
    }

    //跳转到更改页面
    @RequestMapping("/houtai/to_updatefilms")
    public String to_updatefilms(Model model, int userid) {
        List<films> filmsList=new ArrayList<films>();
        filmsList = filmsService.getUserone(userid);
        model.addAttribute("top", "top_films");
        model.addAttribute("show_left", "left_updatefilms");
        model.addAttribute("left", "left_y_films");
        model.addAttribute("mypage", "films");
        model.addAttribute("films", filmsList);
        return "houtai_updatefilms";
    }

    //跳转到添加页面
    @RequestMapping("/houtai/to_addfilms")
    public String to_addfilms(Model model) {
        model.addAttribute("top", "top_films");
        model.addAttribute("show_left", "left_addfilms");
        model.addAttribute("left", "left_y_films");
        model.addAttribute("mypage", "films");
        return "houtai_addfilms";
    }

}
